<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/22
 * Time: 8:59
 */
namespace Admin\Controller;
use Think\Controller;

class HouseController extends Controller
{
    //老胡留下的方法
    public function json11 (){
        if(!S('house')){
            if(!S('unitData') && !S('roomData')){
                $data = D('Room')->relation(true)->select();
    //            echo D('Room')->getLastSql();
                $this->ajaxReturn($data);
                foreach($data as $k=>$v){
                    $c[$v['community_id']]['name'] = $v['community']['name'];

                    $z[$v['zone_id']]['name'] = $v['zone']['name'];
                    $z[$v['zone_id']]['pid'] = $v['community_id'];

                    $b[$v['building_id']]['name'] = $v['building']['name'];
                    $b[$v['building_id']]['pid'] = $v['zone_id'];
                    $b[$v['building_id']]['ppid'] = $v['community_id'];

                    $u[$v['unit_id']]['name'] = $v['unit']['name'];
                    $u[$v['unit_id']]['pid'] = $v['building_id'];
                    $u[$v['unit_id']]['ppid'] = $v['zone_id'];
                    $u[$v['unit_id']]['pppid'] = $v['community_id'];

                    $r[$v['id']]['name'] = $v['name'];
                    $r[$v['id']]['pid'] = $v['unit_id'];
                    $r[$v['id']]['ppid'] = $v['building_id'];
                    $r[$v['id']]['pppid'] = $v['zone_id'];
                    $r[$v['id']]['ppppid'] = $v['community_id'];
                }

                foreach($c as $k=>$v){
                    $result[$k]['community_id'] = $k;
                    $result[$k]['community_name'] = $v['name'];
                }

                foreach($z as $k=>$v){
                    $result[$v['pid']]['data'][$k]['zone_id'] = $k;
                    $result[$v['pid']]['data'][$k]['zone_name'] = $v['name'] ? $v['name'] : '本园区';
                }

                foreach($b as $k=>$v){
                    $result[$v['ppid']]['data'][$v['pid']]['data'][$k]['building_id'] = $k;
                    $result[$v['ppid']]['data'][$v['pid']]['data'][$k]['building_name'] = $v['name'];
                }

                foreach($u as $k=>$v){
                    $result[$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['unit_id'] = $k;
                    $result[$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['unit_name'] = $v['name'] ? $v['name'] : '本单元';
                }
                S('unitData',$result);
                S('roomData',$r);
            }
            if(S('unitData') && S('roomData')){
                $result = S('unitData');
                $r = S('roomData');
                foreach($r as $k=>$v){
                    if($result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]){
                        $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['room_id'] = $k;
                        $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['data'][$k]['room_name'] = $v['name'];
                    }else{
                        $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['unit_id'] = $v['pid'];
                        $result[$v['ppppid']]['data'][$v['pppid']]['data'][$v['ppid']]['data'][$v['pid']]['unit_name'] = '0';
                    }
                }
                S('house',$result);
            }
        }
        header('Access-Control-Allow-Origin: *');
        $res['code'] = 200;
        $res['data'] = S('house');
//        echo '<pre>';
//        var_dump($res);die;
        $this->ajaxReturn($res);
    }

    //根据角色id 返回相应的小区数据
    public function jsonId (){
        $id = I('param.id');
        $id = getCommunityId($id);
        if(!$id){
            $res['code'] = 200;
            $res['data'] = '';
            $this->ajaxReturn($res);
        }
//        echo $id;
        header('Access-Control-Allow-Origin: *');
        $res['code'] = 200;
        $model = new \ZL\Community();
        $res['data'] = $res['data'] = $model->selectCommunity($id);
//        echo '<pre>';
//        var_dump($res['data'][3]);die;
        $this->ajaxReturn($res);
    }
    public function jsonOld ($new=false){
        if(!S('house') || $new){
            $where['status']=1;
            $communityData = D('Community')->where($where)->select();
            $zoneData = D('Zone')->where($where)->select();
            $buildingData = D('Building')->where($where)->select();
            $unitData = D('Unit')->where($where)->select();
            $roomData = D('Room')->where($where)->select();

                foreach ($communityData as $k=>$v) {
                    if($v['children_count']==0)
                        continue;
                    $result[$v['id']]['community_id'] = $v['id'];
                    $result[$v['id']]['community_name'] = $v['name'];

                    foreach ($zoneData as $k1=>$v1) {
                        if($v1['community_id']==$v['id']) {
                            $result[$v['id']]['data'][$v1['id']]['zone_id'] = $v1['id'];
                            $result[$v['id']]['data'][$v1['id']]['zone_name'] = $v1['name'];

                            foreach ($buildingData as $k2 => $v2) {
                                if($v2['zone_id']==$v1['id']){
                                    $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['building_id'] = $v2['id'];
                                    $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['building_name'] = $v2['name'];

                                    foreach ($unitData as $k3 => $v3){
                                        if($v3['building_id']==$v2['id']){
                                            $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['data'][$v3['id']]['unit_id'] = $v3['id'];
                                            $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['data'][$v3['id']]['unit_name'] = $v3['name'];

                                            foreach ($roomData as $k4 => $v4){
                                                if($v4['unit_id']==$v3['id']){
                                                    $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['data'][$v3['id']]['data'][$v4['id']]['room_id'] = $v4['id'];
                                                    $result[$v['id']]['data'][$v1['id']]['data'][$v2['id']]['data'][$v3['id']]['data'][$v4['id']]['room_name'] = $v4['name'];
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            S('house',$result);
        }
        header('Access-Control-Allow-Origin: *');
        $res['code'] = 200;
        $res['data'] = S('house');
//        echo '<pre>';
//        var_dump($res['data'][3]);die;
        $this->ajaxReturn($res);
    }

    public function json ($new=false){
        header('Access-Control-Allow-Origin: *');
        $res['code'] = 200;
        $model = new \ZL\Community();
        if (!$new)
            $res['data'] = $model->getAllCommunity();
        else
            $res['data'] = $model->restAll();
//        S('house',$res['data']);
//        echo '<pre>';
//        var_dump($res['data'][31]);die;
        $this->ajaxReturn($res);
    }

    public function getCommunity(){
        if(!S('communityData')) {
            $data = M('Community')->field('id,name')->select();
            S('communityData', $data, 7200);
        }
        $this->ajaxReturn(S('communityData'));
    }
    public function getData(){
        $type =  I('type');
        if($type==1){
            $data = M('Community')->select();
        }
        $this->ajaxReturn($data);
    }
}